<!--页面主要内容-->
<main class="lyear-layout-content">

  <div class="container-fluid p-t-15">

    <div class="row">
      <div class="col-lg-12">
        <div class="card">
          <div class="card-body">

            <form method="post" class="row" enctype="multipart/form-data">

              <div class="form-group col-md-12">
                <label for="name">分类名称</label>
                <input type="text" class="form-control" id="name" name="name" value="" placeholder="请输入分类名称" />
              </div>

              <div class="form-group col-md-12">
                <label for="weigh">分类权重</label>
                <input type="text" class="form-control" id="weigh" name="weigh" value="" placeholder="请输入分类等级" />
              </div>

              <div class="form-group col-md-12">
                <label>图片上传</label>
                <input type="file" class="form-control" name="cover" id="cover" hidden>
                <div class="input-group" style="display: block;">
                  <label for="cover" onclick="upload('#cover','.img')" style="cursor: pointer;">
                    <img class="img" src="/static/home/res/images/upload.png" id="img-cover" width="200">
                  </label>
                </div>
              </div>

              <div class="form-group col-md-12">
                <button type="" class="btn btn-primary" id="onSubmit">确 定</button>
                <a type="button" class="btn btn-default" href="{:url('admin/Product/cate')}">返
                  回</a>
              </div>
            </form>

          </div>
        </div>
      </div>

    </div>

  </div>

</main>
<!--End 页面主要内容-->

<script>

  // 图片预览函数
  function GetObjectURL (file) {
    // createObjectURL  给一个文件对象 可以提取出一个url本地地址出来
    var url = null;

    //判断是否为IE浏览器还是google还是其他浏览器
    if (window.createObjectURL != undefined) {
      url = window.createObjectURL(file)
    } else if (window.URL != undefined) {
      url = window.URL.createObjectURL(file)
    } else if (window.webkitURL != undefined) {
      url = window.webkitURL.createObjectURL(file)
    }

    return url
  }

  function upload (elem, img) {
    // 点击拉起上传选择
    $(`${elem}`).click(function (param) {
    })

    $(elem).change(function () {
      let file = $(this)[0].files[0] ? $(this)[0].files[0] : {}
      let id = $(this).attr('id')
      let url = GetObjectURL(file)
      let imgArr = $(img);
      for (let item of imgArr) {
        let itemid = $(item).attr('id')
        if ('img-' + id == itemid) {
          $(item).attr('src', url)
        }
      }
    })
  }

</script>